;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Report.PurchaseAmountAnalysis", [])
        .controller("PurchaseAmountAnalysisCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$filter", "$timeout", "UserContextService", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $filter, $timeout, UserContextService) {
            // 分页信息
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            // 标签页信息
            $scope.activeFlag = [true, false];

            // 高级搜索配置
            $scope.toolbarConfig = {
                showColConfig: false
            };

            $scope.tbConf = {
                scrollWidth: 450,
                tableCol: [
                    {
                        name: "代理商",
                        dataField: "purchaseOrgName",
                        width: 150,
                        isShow: true,
                        isFixed: true
                    },
                    {
                        name: "月度订单金额",
                        dataField: "monthAmount",
                        width: 150,
                        isShow: true,
                    },
                    {
                        name: "紧急订单金额",
                        dataField: "urgentAmount",
                        width: 150,
                        isShow: true,
                    },
                ]
            };

            $scope.exportParam = {
                sectionUrl: ybHttp.RequestUrl("device", "/report/exportPurchaseAmountAnalysis"),
            };

            $scope.datePickerFlag = [false, false];

            // 搜索条件
            $scope.condition = {};

            // 数据列表
            $scope.dataList = [];

            // 单行总计
            $scope.totalRow = [];

            $scope.switchDataPicker = function (index) {
                $scope.datePickerFlag = [false, false];
                if (angular.isDefined(index)) {
                    $scope.datePickerFlag[index] = true;
                }
            };

            $scope.checkDate = function (obj, startFiled, endFiled) {
                var timeFormatObj = ybWidget.CheckDate(obj[startFiled], obj[endFiled]);
                if (!!timeFormatObj.tStart) {
                    obj[startFiled] = timeFormatObj.tStart;
                }
                if (!!timeFormatObj.tEnd) {
                    obj[endFiled] = timeFormatObj.tEnd;
                }
            };

            // 切换高级搜索
            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }
            };

            //重置搜索条件
            $scope.reset = function () {
                $scope.condition = {};
                $scope.searchData(1);
            };

            // 按钮权限code
            var btnPermissionCode = {
                purchaseAmountAnalysisExport: window.ActionCode.purchaseAmountAnalysisExport
            };

            // 按钮权限
            $scope.btnPermission = {
                purchaseAmountAnalysisExport: true
            };

            // 设置按钮权限
            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
            }

            // 获取报表列表
            $scope.searchData = function (curPage) {
                var url = '/report/purchaseAmountAnalysis';
                if (curPage) $scope.pageInfo.curPage = curPage;
                var params = {
                    filters: angular.copy($scope.condition),
                    pageSize: $scope.pageInfo.pageSize,
                    pageNum: $scope.pageInfo.curPage
                };
                ybHttp.Post('device', url, params).then(function (res) {
                    if (res.code !== 0) {
                        ybWidget.ShowMessage(res, MESG_TYPE.Error);
                        return;
                    }
                    $scope.totalRow = res.value.list.splice(0, 1);
                    $scope.pageInfo.totalRows = res.value.total;
                    $scope.pageInfo.totalPages = res.value.pages;
                    $scope.dataList = res.value.list;
                })
            };

            (function () {
                UserContextService.SetBtnPermission(setBtnPermission);
            })()
        }])
    ;
})(window, window.angular);